package com.qf.controller;

import com.qf.pojo.Subject;
import com.qf.pojo.User;
import com.qf.service.SubjectService;
import com.qf.service.UserService;
import com.qf.videos.utils.ImageCut;
import com.qf.videos.utils.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

import static com.qf.videos.utils.MailUtils.getValidateCode;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private SubjectService subjectService;

    //首页
    @RequestMapping("show")
    public String show(Model model) {

        List<Subject> subjectList = subjectService.findAllSubject();
        model.addAttribute("subjectList", subjectList);
        return "before/index.jsp";
    }

    //登录
    @RequestMapping("loginUser")
    @ResponseBody
    public String loginUser(String email, String password, HttpSession session) {

        User user = userService.findByEmail(email);
        if (user != null) {
            if (password.equals(user.getPassword())) {

                session.setAttribute("userAccount", email);
                return "success";
            }
        }

        return null;
    }

    //忘记密码
    @RequestMapping("forgetPassword")
    public String forgetPassword() {
        return "before/forget_password.jsp";
    }

    //发送邮件
    @RequestMapping("sendEmail")
    @ResponseBody
    public String sendEmail(String email) {

        User user = userService.findByEmail(email);

        if (user == null) {
            return "hasNoUser";
        } else {
            String code = getValidateCode(6);

            MailUtils.sendMail(email, "你好，这是一封测试邮件，无需回复。", "测试邮件随机生成的验证码是：" + code);

            user.setCode(code);
            userService.update(user);

            return "success";
        }
    }

    //跳转重设密码
    @RequestMapping("validateEmailCode")
    public String validateEmailCode(String email, String code, Model model) {
        model.addAttribute("email", email);

        User user = userService.findByEmail(email);
        if (user.getCode().equals(code)) {
            return "before/reset_password.jsp";
        } else {
            return "redirect:forgetPassword";
        }

    }

    //重设密码
    @RequestMapping("resetPassword")
    public String resetPassword(User user) {

        User userbyemail = userService.findByEmail(user.getEmail());

        user.setId(userbyemail.getId());
        userService.update(user);

        return "before/index.jsp";
    }

    //邮箱判断
    @RequestMapping("validateEmail")
    @ResponseBody
    public String validateEmail(String email) {

        User user = userService.findByEmail(email);
        if (user == null) {
            return "success";
        } else {
            return "false";
        }

    }

    //注册
    @RequestMapping("insertUser")
    @ResponseBody
    public String insertUser(User user, HttpSession session) {

        userService.insert(user);
        session.setAttribute("userAccount", user.getEmail());

        return "success";
    }

    //退出
    @RequestMapping("loginOut")
    public String loginOut(HttpSession session) {

        //销毁Session
        session.invalidate();

        return "redirect:show";
    }

    //个人中心
    @RequestMapping("showMyProfile")
    public String findById(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession(false);
        String email = (String) session.getAttribute("userAccount");

        User user = userService.findByEmail(email);

        model.addAttribute("user", user);

        return "before/my_profile.jsp";

    }

    //个人中心退出
    @RequestMapping("loginOut2")
    public String loginOut2(HttpServletRequest request) {
        HttpSession session = request.getSession(false);

        //销毁Session
        session.invalidate();

        return "redirect:show";
    }

    //更改资料
    @RequestMapping("changeProfile")
    public String changeProfile(HttpSession session, Model model) {
        String email = (String) session.getAttribute("userAccount");

        User user = userService.findByEmail(email);

        model.addAttribute("user", user);

        return "before/change_profile.jsp";
    }

    //更改资料。。。
    @RequestMapping("updateUser")
    public String updateUser(User user) {

        userService.update(user);
        return "redirect:showMyProfile";
    }

    //更改头像
    @RequestMapping("changeAvatar")
    public String changeAvatar(HttpSession session, Model model) {
        String email = (String) session.getAttribute("userAccount");

        User user = userService.findByEmail(email);

        model.addAttribute("user", user);

        return "before/change_avatar.jsp";
    }

    //上传头像
    @RequestMapping("upLoadImage")
    public String upLoadImage(Integer id, MultipartFile image_file, String x1, String x2, String y1, String y2) {
        //上传地址
        String path = "E:\\JavaEE2005\\Server\\apache-tomcat-8.5.41\\webapps\\upload\\";
        //上传的文件名
        String filename = image_file.getOriginalFilename();
        //上传文件
        File file = new File(path + filename);
        //上传方法
        try {
            image_file.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        int x1Int = (int) Double.parseDouble(x1);
        int x2Int = (int) Double.parseDouble(x2);
        int y1Int = (int) Double.parseDouble(y1);
        int y2Int = (int) Double.parseDouble(y2);

        new ImageCut().cutImage(path + "/" + filename, x1Int, y1Int, x2Int - x1Int, y2Int - y1Int);

        User user = new User();
        //设置文件path属性
        user.setImgUrl(filename);
        user.setId(id);

        userService.update(user);

        return "redirect:changeAvatar";
    }


    //密码安全
    @RequestMapping("passwordSafe")
    public String passwordSafe(HttpSession session, Model model) {
        String email = (String) session.getAttribute("userAccount");

        User user = userService.findByEmail(email);

        model.addAttribute("user", user);

        return "before/password_safe.jsp";
    }

    //判断旧密码是否输入正确
    @RequestMapping("validatePassword")
    @ResponseBody
    public String validatePassword(String password, HttpSession session) {
        String email = (String) session.getAttribute("userAccount");

        User user = userService.findByEmail(email);

        if (user != null) {
            if (password.equals(user.getPassword())) {
                return "success";
            } else {
                return "false";
            }
        }
        return null;
    }

    //更新密码
    @RequestMapping("updatePassword")
    public String updatePassword(HttpServletRequest request) {
        String oldPassword = request.getParameter("oldPassword");
        String newPassword = request.getParameter("newPassword");

        User user = new User();
        user.setPassword(newPassword);

        userService.updatePsw(oldPassword, user);

        return "redirect:showMyProfile";
    }

}
